// interceptors/response.interceptor.ts
import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
  Logger,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';

@Injectable()
export class ResponseInterceptor implements NestInterceptor {
  private readonly logger = new Logger(ResponseInterceptor.name);

  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    // const req: any = context.switchToHttp().getRequest();
    return next.handle().pipe(
      map((data: any) => ({ code: 0, data, message: 'success' })),
      tap(() => {
        // 日志记录
        // this.logger.log(`Response: [${req.method}] ${req.url}`);
      }),
    );
  }
}
